/**
 * 全屏中部提示条;
 */
import uiPanel from "./../../lib/UiPanel";
const { ccclass, property } = cc._decorator;

@ccclass
export default class Tips extends uiPanel {
	@property(cc.Node)
	bg: cc.Node = null;

	@property(cc.Label)
	content: cc.Label = null;

	@property(cc.Node)
	contentList: cc.Node = null;

	_item_pool: cc.NodePool = null;

	onLoad() {
		this.node.setPosition(0, 0);
		this.bg.x = 0;
		this.bg.y = 0;
		this.node.active = false;
		this._item_pool = new cc.NodePool();
	}
	onDestroy() {
		this._item_pool = null;
	}

	init(args) {
		this.node.zIndex = 999;
		this.node.active = true;
		this.add(args[0]);
	}

	awake(args) {
		this.add(args[0]);
	}

	add(str) {
		var node: cc.Node = null;
		if (this.contentList.childrenCount > 0) {
			node = this.contentList.children[0];
		} else {
			node = cc.instantiate(this.bg);
		}
		node.parent = this.contentList;
		var contentNode = node.children[0];
		var contentLb = contentNode.getComponent("cc.Label");
		// if (str == contentLb.string) return;
		contentLb.string = str || "no string tip!";
		contentLb._forceUpdateRenderData(true);
		contentNode.width = contentLb.node.width;

		let p = 500;
		let dur = 0.2;
		if (cc.winSize.width > cc.winSize.height) { //横屏
			p = cc.winSize.height / 4;
		}
		let offy = 100;
		node.active = true;
		node.stopAllActions();
		node.setPosition(0, -p);
		node.setScale(0);
		node.opacity = 0;
		var delay = cc.delayTime(1);
		var moveBy = cc.moveBy(dur, cc.v2(0, offy));
		let mb = cc.moveBy(dur, cc.v2(0, -offy));
		let st_0 = cc.scaleTo(dur, 1);
		let st_1 = cc.scaleTo(dur, 0);
		let fi = cc.fadeIn(dur);
		let fo = cc.fadeOut(dur);
		let spw_0 = cc.spawn(moveBy, st_0, fi);
		let spw_1 = cc.spawn(mb, st_1, fo);
		var seq = cc.sequence(spw_0, delay, cc.callFunc(function (target) {
			node.runAction(cc.sequence(spw_1, cc.callFunc(function () {
				this._item_pool.put(node);
				if (this.contentList.children.length == 0) {
					cc.vv.UIMgr.close(cc.vv.GLConfig.tips_path, cc.vv.UIMgr.closeType.destory);
					cc.vv.GLSDKMgr.BACKING = false;
				}
			}.bind(this))));
		}.bind(this)));
		node.runAction(seq);
	}

	update(dt) { }
}
